package com.two.screen.water.balance.widget.dialog

import android.annotation.SuppressLint
import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.view.Gravity
import android.view.KeyEvent
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.SimpleItemAnimator
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.jakewharton.rxbinding2.view.RxView
import com.two.screen.water.balance.R
import com.two.screen.water.balance.bean.water.PayTypeBean
import com.two.screen.water.balance.ui.App
import com.two.screen.water.balance.utils.CommUtils
import com.two.screen.water.balance.utils.DialogUtil
import com.two.screen.water.balance.utils.ToastUtil
import com.two.screen.water.balance.widget.DividerItemDecoration
import me.grantland.widget.AutofitTextView
import java.util.concurrent.TimeUnit


class SelectedPayDialog(var mContext: Context) : Dialog(mContext, R.style.ChooseDialog) {

    private var mSelectedPayDialogListener: SelectedPayDialogListener? = null
    private var mTitleTv: TextView? = null
    private var mDialogCancelTv: TextView? = null
    private var mDialogCollectTv: TextView? = null
    private var mCommonRecyclerView: RecyclerView? = null
    lateinit var mSelectedPayDialogAdapter: SelectedPayDialogAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.dialog_selected_pay)
        this.setCanceledOnTouchOutside(false)

        val window = window
        if (window != null) {
            val attributes = window.attributes
            attributes.width = App.screenWidth
            window.attributes = attributes
            window.setGravity(Gravity.CENTER)
        }

        mDialogCancelTv = findViewById(R.id.mDialogCancelTv)
        mDialogCollectTv = findViewById(R.id.mDialogCollectTv)
        mTitleTv = findViewById(R.id.dialog_title_tv)
        mCommonRecyclerView = findViewById(R.id.mCommonRecyclerView)

        mDialogCancelTv?.setOnClickListener { dismiss() }
        initRecyclerView()
    }

    @SuppressLint("CheckResult")
    private fun initRecyclerView() {
        (mCommonRecyclerView?.itemAnimator as SimpleItemAnimator).supportsChangeAnimations = false //去闪烁
        mCommonRecyclerView?.addItemDecoration(DividerItemDecoration(mContext, DividerItemDecoration.HORIZONTAL_LIST, CommUtils.dp2px(5), ContextCompat.getColor(App.context!!, R.color.transparent)))
        mSelectedPayDialogAdapter = SelectedPayDialogAdapter()
        mCommonRecyclerView?.layoutManager = GridLayoutManager(mContext, 2)
        mCommonRecyclerView?.adapter = mSelectedPayDialogAdapter
        mSelectedPayDialogAdapter.bindToRecyclerView(mCommonRecyclerView)
        mSelectedPayDialogAdapter.setEmptyView(R.layout.layout_status_layout_manager_empty1)

        var payList = mutableListOf(
            PayTypeBean().apply {
                this.type = PayTypeBean.PAY_TYPE_REMAIN
                this.name = PayTypeBean.getPayTypeName(PayTypeBean.PAY_TYPE_REMAIN)
                isLocalSelected = true
            },
            PayTypeBean().apply {
                this.type = PayTypeBean.PAY_TYPE_OWE
                this.name = PayTypeBean.getPayTypeName(PayTypeBean.PAY_TYPE_OWE)
            },
            PayTypeBean().apply {
                this.type = PayTypeBean.PAY_TYPE_JUHE
                this.name = PayTypeBean.getPayTypeName(PayTypeBean.PAY_TYPE_JUHE)
            },
            PayTypeBean().apply {
                this.type = PayTypeBean.PAY_TYPE_SCAN
                this.name = PayTypeBean.getPayTypeName(PayTypeBean.PAY_TYPE_SCAN)
            }/*,
            PayTypeBean().apply {
                this.id = "3"
                this.name = "聚合支付"
            },
            PayTypeBean().apply {
                this.id = "4"
                this.name = "扫码支付"
            },*/
        )
        mSelectedPayDialogAdapter.setNewData(payList)

        mDialogCollectTv?.let {
            RxView.clicks(it)
                .throttleFirst(1, TimeUnit.SECONDS)
                .subscribe {
                    val find = mSelectedPayDialogAdapter.data.find { it.isLocalSelected }
                    if (find == null) {
                        ToastUtil.showToast("请选择支付方式!")
                        return@subscribe
                    }
                    mSelectedPayDialogListener?.onSelected(find)
                }
        }
    }

    /*商品列表 适配器*/
    inner class SelectedPayDialogAdapter : BaseQuickAdapter<PayTypeBean, BaseViewHolder>(R.layout.item_dialog_selected_pay, null) {
        @SuppressLint("CheckResult", "NotifyDataSetChanged")
        override fun convert(helper: BaseViewHolder, item: PayTypeBean) {
            helper.getView<ImageView>(R.id.item_choose_iv).setImageResource(
                when (item.type) {
                    1 -> R.drawable.selected_pay_icon_yu_e
                    2 -> R.drawable.selected_pay_icon_she_zhang
                    3 -> R.drawable.selected_pay_icon_ju_he
                    4 -> R.drawable.selected_pay_icon_scan
                    else -> R.drawable.ic_default_empty
                }
            )

            helper.getView<AutofitTextView>(R.id.item_atv1).text = item.name
            helper.getView<View>(R.id.item_root_view).apply {
                this.isSelected = item.isLocalSelected
                setOnClickListener {
                    mData.forEach { it.isLocalSelected = false }
                    item.isLocalSelected = true
                    notifyDataSetChanged()
                }
            }
//            /*商品 选择*/
//            choseIv.setImageResource(if (item.isLocalSelected) R.drawable.svg_choose_selected else R.drawable.svg_choose_unselected)
        }
    }


    fun setSelectedPayDialogListener(l: SelectedPayDialogListener) {
        mSelectedPayDialogListener = l
    }


    interface SelectedPayDialogListener {
        fun onSelected(item: PayTypeBean)
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
        if (event.device?.name?.contains("USB", true) != true) return false
        if (event.action == KeyEvent.ACTION_DOWN) {//处理触发两次
            when (keyCode) {
                75 -> {// d("确认/'")
                    mDialogCollectTv?.performClick()
                    return true
                }
            }
        }
        return super.onKeyDown(keyCode, event)
    }

    override fun show() {
        super.show()
        //隐藏状态栏
        window?.let { DialogUtil.hideNavigationBar(it) }
    }
}
